SET(CPP_SOURCE_WIX_DIR ${CMAKE_SOURCE_DIR}/wininstall)

# Get revision number
IF(WITH_REVNO)
  EXECUTE_PROCESS(COMMAND git log HEAD^^..HEAD
                  COMMAND FINDSTR commit
  OUTPUT_VARIABLE revno)
  STRING(REPLACE "commit " "" revno ${revno})
  STRING(REPLACE "\n" "" revno ${revno})
ENDIF()

IF(NOT WIX_DIR)
  SET(WIX_DIR "$ENV{WIX}/bin/")
  MESSAGE(STATUS "WiX directory: ${WIX_DIR}")
ENDIF()
SET(PRODUCT_NAME "MariaDB C++ Connector")
#SET(PRODUCT_NAME "${PROJECT_DESCRIPTION}")
SET(PRODUCT_MANUFACTURER "MariaDB")
SET(PRODUCT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")

SET(TLS_LIB_BEGIN "!-- ")
SET(TLS_LIB_END " --")
IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
  SET(PRODUCT_NAME "${PRODUCT_NAME} 64-bit")
  SET(PLATFORM "win64")
  SET(IS_WIN64 "yes")
  SET(IS64 "64")
  SET(WIXPLATFORM "x64")
  SET(FOLDER "ProgramFiles64Folder")
  SET(GUID_REGISTRY "D753D05F-174E-4177-96EE-949513FAD21D")
  SET(GUID_DRIVER "E850E7A3-AD51-40b4-B308-4045DF25D0BE")
  SET(GUID_DEBUG "B65F7C73-58BD-4c02-9EE5-F2C26DAC420E")
  SET(GUID_PLUGINS "A0DB6358-383B-447c-9014-750F74266E3F")
  SET(GUID_PLUGINS_DEBUG "D82D012A-12F2-4c18-B671-53D472770A79")
  SET(GUID_INCLUDES "821B249F-1752-4875-9E0D-BD1A3519146E")
  SET(GUID_INCLUDESCLASSES "bf6a1c65-a74e-454f-ba0f-bcd309a8ddeb")
  SET(GUID_COMPATINCLUDES "C7262456-C6AC-4609-B16C-0886B9ECFF85")
ELSE()
  SET(PLATFORM "win32")
  SET(IS_WIN64 "no")
  SET(IS64 "")
  SET(WIXPLATFORM "x86")
  SET(FOLDER "ProgramFilesFolder")
  SET(GUID_REGISTRY "9E66FC62-480F-440b-8A61-A3ADA91FD6BC")
  SET(GUID_DRIVER "269945E4-8776-4505-A08B-30C7ED15C065")
  SET(GUID_DEBUG "BE46408D-BFA9-4df3-8A84-229A12494284")
  SET(GUID_PLUGINS "59E15495-7EF1-4781-9229-FF246D074BDB")
  SET(GUID_PLUGINS_DEBUG "454A26D3-51B8-435f-AD56-E68E5E70FB9F")
  SET(GUID_INCLUDES "9D136CA2-8AC8-4f0b-A1F4-9A20DC01439D")
  SET(GUID_INCLUDESCLASSES "61aa2ec2-190b-4356-992a-246cfb98b491")
  SET(GUID_COMPATINCLUDES "95AA0630-3A73-4efa-A314-7F287824748D")
ENDIF()

CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/wininstall/mariadb_conncpp.xml.in
               ${CMAKE_BINARY_DIR}/wininstall/mariadb_conncpp.xml)
IF(${revno})
  SET(MSI_PACKAGE "mariadb-connector-cpp-${PRODUCT_VERSION}-r${revno}-${PLATFORM}.msi")
ELSE()
  SET(MSI_PACKAGE "mariadb-connector-cpp-${PRODUCT_VERSION}-${PLATFORM}.msi")
ENDIF()

SET(ENV{MARIADB_CPP_MSI_PACKAGE} "${MSI_PACKAGE}")

IF(WITH_SIGNCODE)
  IF(EXISTS "/tools/sign.bat")
    ADD_CUSTOM_TARGET(SIGNMSI
      DEPENDS ${MSI_PACKAGE}
      COMMAND /tools/sign.bat ${MSI_PACKAGE})
  ELSE()
    ADD_CUSTOM_TARGET(SIGNMSI
      DEPENDS ${MSI_PACKAGE}
      COMMAND signtool sign ${SIGN_OPTIONS} ${MSI_PACKAGE})
  ENDIF()
  ADD_DEPENDENCIES(SIGNMSI ${MSI_PACKAGE})
  SET_TARGET_PROPERTIES(SIGNMSI PROPERTIES EXCLUDE_FROM_ALL OFF)
ENDIF()

MESSAGE(STATUS "MSI package name ${MSI_PACKAGE}")

ADD_CUSTOM_TARGET(
        ${MSI_PACKAGE}
        COMMAND ${WIX_DIR}light.exe -ext WixUIExtension -ext WixUtilExtension mariadb_conncpp.wixobj -o ${MSI_PACKAGE})

SET_TARGET_PROPERTIES(${MSI_PACKAGE} PROPERTIES EXCLUDE_FROM_ALL OFF)

ADD_CUSTOM_TARGET(
        CPP_WIX
        DEPENDS mariadb_conncpp.xml
        COMMAND ${WIX_DIR}candle.exe -ext WixUIExtension -ext WixUtilExtension mariadb_conncpp.xml -o mariadb_conncpp.wixobj)

ADD_DEPENDENCIES(${MSI_PACKAGE} CPP_WIX)
ADD_DEPENDENCIES(CPP_WIX ${LIBRARY_NAME} ${STATIC_LIBRARY_NAME})
IF(NOT USE_SYSTEM_INSTALLED_LIB)
  ADD_DEPENDENCIES(CPP_WIX ${LIBRARY_NAME} dialog caching_sha2_password sha256_password mysql_clear_password)
ENDIF()

